package com.woniuxy.joylifeshop.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.common.entity.ShopCommodity;
import com.woniuxy.common.entity.ShopCommodityImage;
import com.woniuxy.common.entity.ShopCommodityType;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.CommodityException;
import com.woniuxy.joylifeshop.feign.ForbiddenWordsFeign;
import com.woniuxy.joylifeshop.service.CommodityImageService;
import com.woniuxy.joylifeshop.service.CommodityService;
import com.woniuxy.joylifeshop.service.CommodityTypeService;
import com.woniuxy.joylifeshop.service.MerchantRepositoryService;
import com.woniuxy.joylifeshop.vo.ResponseInformation;
import com.woniuxy.joylifeshop.vo.ViewObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class CommodityController {

    @Autowired
    CommodityService commodityService;

    @Autowired
    CommodityTypeService commodityTypeService;

    @Autowired
    CommodityImageService commodityImageService;

    @Autowired
    ForbiddenWordsFeign forbiddenWordsFeign;

    @Autowired
    MerchantRepositoryService merchantRepositoryService;

    /**
    * @Author SooYoo
    * @Description 上传商品
    * @Param
    * @Return
    **/
    @RequestMapping("/addMerchantCommodity")
    public ViewObject addMerchantCommodity(@RequestBody String information){
        System.out.println(information);
        ViewObject viewObject = new ViewObject();
        viewObject.setCode(0);
        viewObject.setCount(1);
        viewObject.setData(null);

        ShopCommodity shopCommodity = JSON.parseObject(information, ShopCommodity.class);
        ShopCommodityType shopCommodityType = JSON.parseObject(information, ShopCommodityType.class);

        //验证类别是否错误
        String typeCheck = commodityTypeService.checkShopCommodityType(shopCommodityType);
        if (typeCheck=="未找到"){
            viewObject.setMsg("未识别的商品类型");
            return  viewObject;
        }
        List commodityInfo = new ArrayList();
        commodityInfo.add(shopCommodity.getName());
        commodityInfo.add(shopCommodity.getDescription());
        //发布前用feign
        Integer integer = forbiddenWordsFeign.checkWords(commodityInfo);
        if (integer==2){
            viewObject.setMsg("含有违禁词");
            return viewObject;
        }
        //存入一个商品对象
        ShopCommodity shopCommodityAfter = commodityService.addOneCommodity(shopCommodity);
        //给商品配套图片对象
        ShopCommodityImage shopCommodityImage = commodityImageService.jointImageObject();
        //给商品对象附加图片对象id
        shopCommodityAfter.setImgId(shopCommodityImage.getImgId());
        //更新商品对象
        commodityService.addCommodityImgid(shopCommodityAfter);
        //配套仓储对象
        merchantRepositoryService.addRepositoryObject(shopCommodity);

        viewObject.setMsg("添加商品成功");
        return viewObject;
    }



    /**
    * @Author SooYoo
    * @Description 通过商品id来初始化商品详情页面
    * @Param
    * @Return
    **/
    @RequestMapping("/initDetail")
    public ResponseInformation initDetail(String shopId) {

        ResponseInformation responseInformation = new ResponseInformation();
        try {

            ObjectDto objectDto = commodityService.readCommodityById(shopId);
            responseInformation.setData(objectDto);
            responseInformation.setMsg("success");
            responseInformation.setState(200);
            return responseInformation;

        } catch (CommodityException e) {

            e.printStackTrace();
            responseInformation.setMsg("fail");
            responseInformation.setState(500);
            return responseInformation;
        }
    }


    /**
    *@Author SooYoo
    *@Description  实现检索功能
    *@Param
    *@Return
    **/
    @RequestMapping("/search")
    public ResponseInformation searchCommodity(String name){

        ResponseInformation information = new ResponseInformation();
        //System.out.println(name);

        try {
            //找到商品集合
            List<ObjectDto> objectDtos = commodityService.readCommodityByName(name);
            //充填
            information.setData(objectDtos);
            information.setMsg("ok");
            information.setState(200);
            return information;

        } catch (CommodityException e) {
            e.printStackTrace();

            information.setState(500);
            information.setMsg("搜索错误");
            return information;
        }

    }



}
